package zhaoziming;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Lottery {
    public static void main(String[] args) {
        int[] userNumbers = getUserInput();
        int[] winningNumbers = generateWinningNumbers();

        // 输出中奖号码
        System.out.print("本期中奖号码为：红球：");
        for (int i = 0; i < 6; i++) {
            System.out.print(winningNumbers[i] + " ");
        }
        System.out.println("蓝球：" + winningNumbers[6]);

        // 输出用户输入的号码
        System.out.print("您输入的号码为：红球：");
        for (int i = 0; i < 6; i++) {
            System.out.print(userNumbers[i] + " ");
        }
        System.out.println("蓝球：" + userNumbers[6]);

        int redBallMatch = countRedBallMatch(winningNumbers, userNumbers);
        int blueBallMatch = (winningNumbers[6] == userNumbers[6])? 1 : 0;
        printResult(redBallMatch, blueBallMatch);
    }

    public static int[] generateWinningNumbers() {
        Random random = new Random();
        List<Integer> redBalls = new ArrayList<>();
        while (redBalls.size() < 6) {
            int num = random.nextInt(33) + 1;
            if (!redBalls.contains(num)) {
                redBalls.add(num);
            }
        }
        int blueBall = random.nextInt(16) + 1;
        int[] numbers = new int[7];
        for (int i = 0; i < 6; i++) {
            numbers[i] = redBalls.get(i);
        }
        numbers[6] = blueBall;
        return numbers;
    }

    public static int[] getUserInput() {
        Scanner scanner = new Scanner(System.in);
        int[] numbers = new int[7];
        System.out.println("请依次输入6个红球号码（1 - 33）和1个蓝球号码（1 - 16）：");
        for (int i = 0; i < 6; i++) {
            System.out.println("请输入红球号码" + (i + 1) + "（1 - 33）：");
            while (true) {
                int num = scanner.nextInt();
                if (num >= 1 && num <= 33) {
                    boolean isDuplicate = false;
                    for (int j = 0; j < i; j++) {
                        if (numbers[j] == num) {
                            isDuplicate = true;
                            break;
                        }
                    }
                    if (!isDuplicate) {
                        numbers[i] = num;
                        break;
                    } else {
                        System.out.println("号码重复，请重新输入：");
                    }
                } else {
                    System.out.println("号码超出范围，请重新输入：");
                }
            }
        }
        System.out.println("请输入蓝球号码（1 - 16）：");
        while (true) {
            int num = scanner.nextInt();
            if (num >= 1 && num <= 16) {
                numbers[6] = num;
                break;
            } else {
                System.out.println("号码超出范围，请重新输入：");
            }
        }
        scanner.close();
        return numbers;
    }

    public static int countRedBallMatch(int[] winningNumbers, int[] userNumbers) {
        int count = 0;
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                if (winningNumbers[i] == userNumbers[j]) {
                    count++;
                }
            }
        }
        return count;
    }

    public static void printResult(int redBallMatch, int blueBallMatch) {
        if (redBallMatch == 6 && blueBallMatch == 1) {
            System.out.println("恭喜您，中了一等奖！");
        } else if (redBallMatch == 6 && blueBallMatch == 0) {
            System.out.println("恭喜您，中了二等奖！");
        } else if (redBallMatch == 5 && blueBallMatch == 1) {
            System.out.println("恭喜您，中了三等奖，奖金3000元！");
        } else if ((redBallMatch == 5 && blueBallMatch == 0) || (redBallMatch == 4 && blueBallMatch == 1)) {
            System.out.println("恭喜您，中了四等奖，奖金200元！");
        } else if ((redBallMatch == 4 && blueBallMatch == 0) || (redBallMatch == 3 && blueBallMatch == 1)) {
            System.out.println("恭喜您，中了五等奖，奖金10元！");
        } else if ((redBallMatch == 2 && blueBallMatch == 1) || (redBallMatch == 1 && blueBallMatch == 1) || (redBallMatch == 0 && blueBallMatch == 1)) {
            System.out.println("恭喜您，中了六等奖，奖金5元！");
        } else {
            System.out.println("很遗憾，您未中奖！");
        }
    }
}